package main

import (
	"github.com/gocolly/colly"
	"match-spider/util/pg"
	"strconv"
	"match-spider/util"
	"github.com/json-iterator/go"
	"time"
	"math/rand"
	"fmt"
)

func fetchTips(matchId,date,number string,isFailedData bool)  {
	url:="https://api.fox008.com/mobile/index?app=IPHONE&lineId="+number+"&matchDate="+date+"&v=Z66&wAction=602"

	c := colly.NewCollector()
	c.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"

	c.OnResponse(func(response *colly.Response) {
		fmt.Println(string(response.Body))
		matchKey:=date+number
		json := jsoniter.ConfigCompatibleWithStandardLibrary
		m:=make(map[string]interface{})
		json.Unmarshal(response.Body,&m)
		if m["detail"]==nil{
			return
		}
		detail:=m["detail"].(map[string]interface{})
		homeTips:=detail["homeTips"]
		guestTips:=detail["guestTips"]
		if homeTips==nil&&guestTips==nil{
			return
		}
		homeData,awayData:="",""
		if homeTips!=nil{
			b,_:=json.Marshal(homeTips)
			homeData=string(b)
		}
		if guestTips!=nil{
			b,_:=json.Marshal(guestTips)
			awayData=string(b)
		}
		c:=pg.Count("select count(*) as c from t_match_tips where match_key=$1",matchKey)
		b:=false
		if c>0{
			sql:="update t_match_tips set home_tips=$2,away_tips=$3 where match_key=$1"
			b=pg.UpdateOrDelete(sql,matchKey,homeData,awayData)
		}else{

			sql:="insert into t_match_tips(match_id,match_key,home_tips,away_tips) values($1,$2,$3,$4)"
			b=pg.Insert(sql,matchId,matchKey,homeData,awayData)>=0
		}
		if b&&isFailedData{
			pg.UpdateOrDelete("delete from spider_f_tips where match_id=$1",matchId)
		}
	})
	c.OnRequest(func(r *colly.Request) {

	})
	c.OnError(func(r *colly.Response, err error) {
		fmt.Println(err.Error())
		if !isFailedData{
			pg.Insert("insert into spider_f_tips values($1,$2,$3,$4)",util.GainId(),matchId,number,date)
		}

	})

	c.Visit(url)
}
func dbselect(page int) bool {
	length:=100
	offset := (page - 1) * length

	sql:="select * from t_match where is_jc=1 order by start_at,id limit "+strconv.Itoa(length)+" OFFSET "+strconv.Itoa(offset)
	list:=pg.FindAll(sql)

	if len(list)==0{
		return false
	}
	for _,v:=range list{
		number:=v["jc_number"].(string)
		date:=v["create_date"].(string)
		dateTime,_:=time.Parse("2006-01-02T15:04:05Z",date)
		dateString:=dateTime.Format("20060102")
		number=util.JcNumber(number)
		//fmt.Println(dateString)
		fetchTips(v["id"].(string),dateString,number,false)
		seed:=100+rand.Intn(400)
		time.Sleep(time.Millisecond*time.Duration(seed))
	}
	page++
	fmt.Println("page",page)
	return dbselect(page)
}

func dbfailed(page int) bool {
	length:=100
	offset := (page - 1) * length

	sql:="select * from spider_f_tips order by match_date limit "+strconv.Itoa(length)+" OFFSET "+strconv.Itoa(offset)
	list:=pg.FindAll(sql)

	if len(list)==0{
		return false
	}
	for _,v:=range list{
		number:=v["number"].(string)
		date:=v["match_date"].(string)
		dateString:=date
		number=util.JcNumber(number)
		fetchTips(v["match_id"].(string),dateString,number,true)
		seed:=100+rand.Intn(400)
		time.Sleep(time.Millisecond*time.Duration(seed))
	}
	page++
	fmt.Println("page",page)
	return dbfailed(page)
}

func main()  {

	//dbselect(1)
	dbfailed(1)
}